using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class ArticleSalesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class ArticleSalesDbo : DatabaseObject
        {
            public ArticleSalesDbo()
            {
                Name = "ArticleSales";
                GeneratedClassName = "ArticleSales";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ArticleId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Date", ColumnType = "date", IsIdentity = false, IsNullable = false, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "Date" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Units", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Units" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Sales" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "GM", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "GM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "GrossSales", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "GrossSales" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Price", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Price" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Confirmed", ColumnType = "bit", IsIdentity = false, IsNullable = true, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "Confirmed" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "NetMargin", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "NetMargin" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PIDiffUnits", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "PIDiffUnits" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PIDiff", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "PIDiff" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ChargeOffsUnits", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ChargeOffsUnits" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ChargeOffs", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "ChargeOffs" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Rebates", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Rebates" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "BonusBuyNumber", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "BonusBuyNumber" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Costs", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Costs" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CostsWithVat", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "CostsWithVat" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Deleted", ColumnType = "bit", IsIdentity = false, IsNullable = true, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "Deleted" });
              
            }
        }
		
		public static List<ArticleSaleRow> Select(Expression<Func<ArticleSaleRow, bool>> expression)
        {
            List<ArticleSaleRow> rows = new List<ArticleSaleRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<ArticleSaleRow>(new ArticleSalesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<ArticleSaleRow> Select()
		{
			List<ArticleSaleRow> rows = new List<ArticleSaleRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<ArticleSaleRow>(new ArticleSalesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<ArticleSaleRow> PagedSelect(Expression<Func<ArticleSaleRow, bool>> filterExpression, Expression<Func<ArticleSaleRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<ArticleSaleRow> rows = new List<ArticleSaleRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<ArticleSaleRow>(new ArticleSalesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<ArticleSaleRow> ArticleSaleList)
        {
            ArticleSaleRow row;

            while (reader.Read())
            {
                row = new ArticleSaleRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					StoreId = Util.CastDbValue<int>(reader["StoreId"]),
					ArticleId = Util.CastDbValue<int>(reader["ArticleId"]),
					Date = Util.CastDbValue<DateTime>(reader["Date"]),
					Units = Util.CastDbValue<int>(reader["Units"]),
					Sales = Util.CastDbValue<decimal>(reader["Sales"]),
					GM = Util.CastDbValue<decimal>(reader["GM"]),
					GrossSales = Util.CastDbValue<decimal>(reader["GrossSales"]),
					Price = Util.CastDbValue<decimal?>(reader["Price"]),
					Confirmed = Util.CastDbValue<bool?>(reader["Confirmed"]),
					NetMargin = Util.CastDbValue<decimal?>(reader["NetMargin"]),
					PIDiffUnits = Util.CastDbValue<int?>(reader["PIDiffUnits"]),
					PIDiff = Util.CastDbValue<decimal?>(reader["PIDiff"]),
					ChargeOffsUnits = Util.CastDbValue<int?>(reader["ChargeOffsUnits"]),
					ChargeOffs = Util.CastDbValue<decimal?>(reader["ChargeOffs"]),
					Rebates = Util.CastDbValue<decimal?>(reader["Rebates"]),
					BonusBuyNumber = Util.CastDbValue<string>(reader["BonusBuyNumber"]),
					Costs = Util.CastDbValue<decimal?>(reader["Costs"]),
					CostsWithVat = Util.CastDbValue<decimal?>(reader["CostsWithVat"]),
					Deleted = Util.CastDbValue<bool?>(reader["Deleted"])
                };
                                
                ArticleSaleList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(ArticleSaleRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<ArticleSaleRow>(new ArticleSalesDbo(), row);
            }
        }

		public static void Update(ArticleSaleRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<ArticleSaleRow>(new ArticleSalesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<ArticleSaleRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<ArticleSaleRow>(new ArticleSalesDbo(), expression);
            }
        }

	}
}
	
